#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>

struct Node
{
	char data[10];
	bool isOperator;
	int priority;
};

struct Caculator
{
	char variable;
	char expression[100];
	int value;
	bool isFunction;
	char valueOf;
};

struct CaculatorIndex
{
	char variable;
	struct Caculator* data;
};

extern char* getStr();

extern void removeSpace(char *str);

extern void convertInfix2Posfix(char *infix, Node posfix[100], int &index);

extern int caculate(Node posfix[100], int &index);

int GetInt()
{
	int num;
	scanf("%d", &num);
	return num;
}

void PutInt(int num)
{
	printf("%d", num);
}

void PutStr(char* str)
{
	printf("%s", str);
}

char* addNum(int num, char c, char *str)
{
	int i = 0, a = 0;
	char result[100];
	while(*str != 0)
	{
		if(*str == c)
		{
			char number[10];
			itoa(num, number, 10);
			for(int j = 0; j < strlen(number); j++)
			{
				result[i] = number[j];
				i++;
			}
			i--;
		}
		else
			result[i] = *str;
		str++;
		i++;
	}
	result[i] = '\0';
	char *s = new char[100];
	strcpy(s, result);
	return s;
}

int getValue(char c, CaculatorIndex list[100], int count)
{
	for(int i = 0; i < count; i++)
	{
		if(list[i].variable == c)
			return list[i].data->value;
	}
	return 0;
}

void setValue(char c, CaculatorIndex list[100], int count, int value)
{
	for(int i = 0; i < count; i++)
	{
		if(list[i].variable == c)
		{
			list[i].data->value = value;
			break;
		}
	}
}

void main()
{	
	struct CaculatorIndex list[100];
	int result = 0, len = 0, count = 0;
	int index = 0, value;
	char c;
	Node posfix[100];
	char *infix = new char[100];
	char *caculator = new char[150];

	char *input = "testcase.txt";

	FILE *fp = fopen(input, "r");
	while(fgets(caculator, 100, fp) != NULL)
	{
		Caculator *cal = new Caculator;
		removeSpace(caculator);
		if(caculator[0] >= 97 && caculator[0] <= 122 && caculator[2] != 'G')
		{
			cal->variable = caculator[0];
			len = strlen(caculator);
			for(int i = 2; i < len; i++)
			{
				infix[i-2] = caculator[i];
			}
			infix[len-2] = '\0';
			strcpy(cal->expression, infix);
			cal->isFunction = false;
		}
		else if(caculator[0] == 'P')
		{
			cal->variable = caculator[3];
			strcpy(cal->expression, caculator);
			cal->isFunction = true;
		}
		else if(caculator[0] >= 97 && caculator[0] <= 122 && caculator[2] == 'G')
		{
			Caculator *enter = new Caculator;
			enter->value = 0;
			strcpy(enter->expression, "0");
			enter->variable = caculator[0];
			enter->isFunction = false;
			
			list[count].variable = enter->variable;
			list[count].data = enter;
			count++;

			cal->variable = caculator[2];
			strcpy(cal->expression, caculator);
			cal->isFunction = true;
			cal->valueOf = caculator[0];
		}

		list[count].variable = cal->variable;
		list[count].data = cal;
		count++;
	}
	fclose(fp);

	for(int i = 0; i < count; i++)
	{
		if(!list[i].data->isFunction)
		{
			if(i > 0)
			{
				for(int j = 0; j < strlen(list[i].data->expression); j++)
				{
					char c = list[i].data->expression[j];
					if(c >= 97 && c <= 122)
					{
						char *result = addNum(getValue(c, list, count), c, list[i].data->expression);
						strcpy(list[i].data->expression, result);
					}
				}
			}
			index = 0;
			convertInfix2Posfix(list[i].data->expression, posfix, index);
			list[i].data->value = caculate(posfix, index);
		}
		else
		{
			if(list[i].data->variable == 'I')
			{
				c = list[i].data->expression[7];
				value = getValue(c, list, count);
				PutInt(value);
			}
			if(list[i].data->variable == 'S')
			{
				len = strlen(list[i].data->expression);
				char enter[100];
				int k = 0;
				bool isGet = false;
				int j = 0;
				while(j < len)
				{
					if(list[i].data->expression[j] == '"')
						isGet = !isGet;
					if(isGet && list[i].data->expression[j] == 92 && list[i].data->expression[j+1] == 'n')
					{
						enter[k] = '\n';
						k++;
						j++;
					}
					else if(isGet && list[i].data->expression[j] != '"')
					{
						enter[k] = list[i].data->expression[j];
						k++;
					}
					j++;
				}
				enter[k] = '\0';
				PutStr(enter);
			}
			if(list[i].data->variable == 'G')
			{
				c = list[i].data->valueOf;
				int value = GetInt();
				setValue(c, list, count, value);
			}
		}
	}

	return;
}